package com.szt.sy.controller.system.buttonrights;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.szt.sy.anontation.Authority;
import com.szt.sy.anontation.AuthorityConfig;
import com.szt.sy.anontation.SysLog;
import com.szt.sy.common.AuthorityCommon;
import com.szt.sy.controller.base.AdminBaseController;
import com.szt.sy.entity.system.Button;
import com.szt.sy.entity.system.Role;
import com.szt.sy.entity.system.RoleFhbutton;
import com.szt.sy.service.system.button.ButtonService;
import com.szt.sy.service.system.role.RoleService;
import com.szt.sy.service.system.rolefhbutton.RoleFhbuttonService;
import com.szt.sy.vo.rep.base.RepBaseVO;

/***
 * 按钮权限-控制器
 * 
 * @author wwy
 *
 */
@Controller
@AuthorityConfig(prefix = "/admin/buttonrights/list")
@RequestMapping(value = "admin/buttonrights")
public class ButtonrightsController extends AdminBaseController {
	
	@Autowired
	private RoleFhbuttonService roleFhbuttonService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private ButtonService fhbuttonService;
	
	/**
	 * 列表
	 *
	 * @throws Exception
	 */
	@Authority(name = AuthorityCommon.LIST)
	@RequestMapping(value = "/list")
	public ModelAndView list(String roleId) throws Exception {
		ModelAndView mv = this.getModelAndView();
		Map<String, Object> map = new HashMap<>(3);
		map.put("id", roleId);
		if (StringUtils.isBlank(roleId)) {
			/** 默认列出第一组角色(初始设计系统用户和会员组不能删除) */
			map.put("id", "1");
		}
		Map<String, Object> fmap = new HashMap<>(1);
		fmap.put("id", "0");
		/** 列出组(页面横向排列的一级组) */
		List<Role> roleList = roleService.listAllRolesByPId("0");
		/** 列出此组下架角色 */
		List<Role> roleListZ = roleService.listAllRolesByPId(map.get("id").toString());
		/** 列出所有按钮 */
		List<Button> buttonlist = fhbuttonService.listAll(map);
		/** 列出所有角色按钮关联数据 */
		List<RoleFhbutton> roleFhbuttonlist = roleFhbuttonService.listAll(map);
		/** 取得点击的角色组(横排的) */
		Role role = roleService.findObjectById(map.get("id").toString());
		mv.addObject("pd", role);
		mv.addObject("roleList", roleList);
		mv.addObject("roleList_z", roleListZ);
		mv.addObject("buttonlist", buttonlist);
		mv.addObject("roleFhbuttonlist", roleFhbuttonlist);
		/** 按钮权限 */
		
		mv.setViewName("system/buttonrights/buttonrights_list");
		return mv;
	}
	
	/**
	 * 点击按钮处理关联表
	 *
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@SysLog("按钮权限-修改")
	@Authority(name = AuthorityCommon.EDIT)
	@RequestMapping(value = "/upRb")
	@ResponseBody
	public RepBaseVO updateRolebuttonrightd(String roleId, String buttonId) throws Exception {
		RepBaseVO repVO = new RepBaseVO<>();
		Map<String, String> map = new HashMap<>(1);
		String errInfo = "success";
		RoleFhbutton rolef = roleFhbuttonService.findByRoleId(roleId, buttonId);
		/** 判断关联表是否有数据 是:删除/否:新增 */
		if (null != rolef) {
			/** 删除 */
			roleFhbuttonService.delete(rolef.getId());
		} else {
			/** 新增 */
			rolef = new RoleFhbutton();
			rolef.setButtonId(buttonId);
			rolef.setRoleId(roleId);
			roleFhbuttonService.save(rolef);
		}
		map.put("result", errInfo);
		return repVO.setCommonSuccess().setData(map);
	}
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format, true));
	}
}
